package com.cskaoyan.market.controller;/*
 *文件名: AdminOrderController
 *创建者:wxin
 *创建时间:2024/5/8 21:59
 */


import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.service.MarketStatGoodsService;
import com.cskaoyan.market.service.MarketStatOrderService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketGoodsVo;
import com.cskaoyan.market.vo.MarketOrderVo;
import com.cskaoyan.market.vo.MarketUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDate;
import java.util.*;


@RestController
@RequestMapping("/admin/stat")
public class AdminStatController {
    @Autowired
    private MarketStatOrderService marketOrderService;
    @Autowired

    private MarketStatGoodsService marketGoodsService;

    @Autowired
    private MarketUserMapper mapper;

    @GetMapping("user")

    private Object user() throws IOException {
        List<MarketUser> marketUsers = mapper.selectByExample(new MarketUserExample());
        HashMap<LocalDate, Integer> userNumGroupByLastLoginTime = new HashMap<>();
        //先查表拿数据，处理数据得到一个HashMap
        for (MarketUser marketUser : marketUsers) {
            LocalDate lastLoginTime = marketUser.getLastLoginTime().toLocalDate();
            Integer userNum = userNumGroupByLastLoginTime.getOrDefault(lastLoginTime, 0);
            userNumGroupByLastLoginTime.put(lastLoginTime,userNum + 1);
        }
        //把数据封装到MarketUserVo中
        MarketUserVo marketUserVo = new MarketUserVo();
        marketUserVo.setColumns(Arrays.asList("day","users"));
        List<MarketUserVo.UserNumByDay> userNumByDayList = new ArrayList<>();
        for (Map.Entry<LocalDate, Integer> entry : userNumGroupByLastLoginTime.entrySet()) {
            MarketUserVo.UserNumByDay userNumByDay = new MarketUserVo.UserNumByDay(entry.getKey(), entry.getValue());
            userNumByDayList.add(userNumByDay);
        }
        marketUserVo.setRows(userNumByDayList);

       return ResponseUtil.ok(marketUserVo);
    }

    @GetMapping("goods")

    private Object goods() throws IOException {
        MarketGoodsVo marketGoodsVo = marketGoodsService.getGoods();
        return ResponseUtil.ok(marketGoodsVo);

    }

    @GetMapping("order")
    private Object order() throws IOException {
        MarketOrderVo marketOrderVo = marketOrderService.getOrder();
        return ResponseUtil.ok(marketOrderVo);

    }


}
